## This file creates Medicaid administrative burden indices, ##
## with data compiled by the author using Kaiser Family Foundation annual reports, 
## published in PDF report format here: https://www.kff.org/medicaid/report/annual-updates-on-eligibility-rules-enrollment-and-renewal-procedures-and-cost-sharing-practices-in-medicaid-and-chip/ ##
## and also data provided by Prof Ashley Fox via https://safetynetgenerosity.com (Medicaid generosity index) ##
## Created by Meredith Dost and last run 8/17/2025 ##

# load in packages
library(haven)

# set working directory
#setwd("burden_data/kff/)

#### 2020 data from KFF report ####
k20 <- read.csv("kff_2020.csv")
# clean
kff_clean <- data.frame(matrix(nrow=nrow(k20),ncol=ncol(k20)))
for(i in 2:ncol(k20)){
  x <- k20[,i]
  x[x=="Y"] <- 1
  x[is.na(x)] <- 0
  x[x==""] <- 0
  kff_clean[,i] <- as.numeric(x)
  }
colnames(kff_clean) <- colnames(k20)
kff_clean$State <- k20$State
k20 <- kff_clean
## adding work req var
k20$work_req <- 0
k20$work_req[k20$State=="Indiana"] <- 1
k20$work_req[k20$State=="Utah"] <- 1
k20$work_req[k20$State=="Arizona"] <- 1
k20$work_req[k20$State=="Wisconsin"] <- 1
k20$work_req[k20$State=="Nebraska"] <- 1
k20$work_req[k20$State=="Ohio"] <- 1
k20$work_req[k20$State=="Georgia"] <- 1
k20$work_req[k20$State=="South Carolina"] <- 1
k20$STATE <- k20$State
rm(i,x,kff_clean)

#### 2018 data from KFF report ####
k18 <- read.csv("kff_2018.csv")
# clean
kff_clean <- data.frame(matrix(nrow=nrow(k18),ncol=ncol(k18)))
for(i in 2:ncol(k18)){
  x <- k18[,i]
  x[x=="Y"] <- 1
  x[is.na(x)] <- 0
  x[x==""] <- 0
  kff_clean[,i] <- as.numeric(x)
  }
colnames(kff_clean) <- colnames(k18)
kff_clean$State <- k18$State
k18 <- kff_clean
## adding work req var
k18$work_req <- 0
k18$work_req[k18$State=="Arkansas" | k18$State=="Indiana" | k18$State=="New Hampshire" | k18$State=="Wisconsin"] <- 1
k18$work_req[k18$State=="Kentucky"] <- 1 # adding this temporarily--was struck down by court
k18$STATE <- k18$State
rm(i,x,kff_clean)

#### 2016 data from KFF report ####
k16 <- read.csv("kff_2016.csv")
# clean
kff_clean <- data.frame(matrix(nrow=nrow(k16),ncol=ncol(k16)))
for(i in 2:ncol(k16)){
  x <- k16[,i]
  x[x=="Y"] <- 1
  x[is.na(x)] <- 0
  x[x==""] <- 0
  kff_clean[,i] <- as.numeric(x)
  }
colnames(kff_clean) <- colnames(k16)
kff_clean$State <- k16$State
k16 <- kff_clean
## adding work req var
k16$work_req <- 0
k16$STATE <- k16$State
rm(i,x,kff_clean)

#### Data compiled by Fox et al/SafetyNetGenerosity.com, relying on same KFF reports ####
fox <- read_dta("MGI_All.dta")
# subset only to relevant variables
fox.sub <- fox[,c(1,2,3,12:15,17,19,21:26,28:29,107:122)]
# subset to years of interest
fox.sub <- subset(fox.sub, YEAR>=2010)
rm(fox)
# fixing missing values
fox.sub$WG_Medicaid_Digital_05[fox.sub$STATE=="Tennessee" & fox.sub$YEAR==2018] <- fox.sub$WG_Medicaid_Digital_05[fox.sub$STATE=="Tennessee" & fox.sub$YEAR==2017]
fox.sub$WG_Medicaid_Digital_05[fox.sub$STATE=="Tennessee" & fox.sub$YEAR==2018] <- fox.sub$WG_Medicaid_Digital_05[fox.sub$STATE=="Tennessee" & fox.sub$YEAR==2019]
fox.sub$WG_Medicaid_Digital_07[fox.sub$STATE=="Tennessee" & fox.sub$YEAR==2018] <- fox.sub$WG_Medicaid_Digital_07[fox.sub$STATE=="Tennessee" & fox.sub$YEAR==2017]
# subset only to relevant variables
fox.sub <- fox.sub[,-c(19,31,33)]
# subset to years of interest
fox.sub <- subset(fox.sub, YEAR %in% seq(2010,2020,2))
# rename vars
names(fox.sub)[c(26,20,24,28)] <- c("online_account","state_processes_auto_renewals","renew_coverage","onlineapp_mobile_friendly")

#### Merge datasets together ####
# 2010
f10 <- subset(fox.sub, YEAR==2010)
f10$authorize_thirdpty_access <- 0
f10$upload_verif_docs <- 0
f10$go_paperless <- 0
f10$work_req <- 0
# 2012
f12 <- subset(fox.sub, YEAR==2012)
f12$authorize_thirdpty_access <- 0
f12$upload_verif_docs <- 0
f12$go_paperless <- 0
f12$work_req <- 0
# 2014
f14 <- subset(fox.sub, YEAR==2014)
f14$authorize_thirdpty_access <- 0
f14$upload_verif_docs <- 0
f14$go_paperless <- 0
f14$work_req <- 0
# 2016
f16 <- subset(fox.sub, YEAR==2016)
k16.sub <- k16[,c(3,4,7:9)]
f16.m <- merge(f16,k16.sub, by = c("STATE"))
# 2018
f18 <- subset(fox.sub, YEAR==2018)
k18.sub <- k18[,c(7:11)]
f18.m <- merge(f18,k18.sub, by = c("STATE"))
# 2020
f20 <- subset(fox.sub, YEAR==2020)
k20.sub <- k20[,c(7:11)]
f20.m <- merge(f20,k20.sub, by = c("STATE"))
# merging together
kmerg <- rbind.data.frame(f10,f12,f14,f16.m,f18.m,f20.m)
# reverse code work req var 
kmerg$work_req <- -1*kmerg$work_req + 1

#### Create Medicaid administrative burden measures ####
# My measure
kmerg$sum <- rowSums(kmerg[,4:34])/31
kmerg$negsum <- -1*kmerg$sum + 1
# Fox et al index
kmerg$fox <- rowSums(kmerg[,4:30])/27
kmerg$foxneg <- -1*kmerg$fox + 1
# Reduced index
kmerg$AdBurden3 <- kmerg$renew_coverage + kmerg$onlineapp_mobile_friendly + kmerg$state_processes_auto_renewals + kmerg$upload_verif_docs + kmerg$authorize_thirdpty_access + kmerg$work_req
kmerg$AB3 <- -1*(kmerg$AdBurden3/6)+1

#### Final cleaning and save out ####
# rename variables
kmerg$State <- kmerg$STATE
kmerg$year <- kmerg$YEAR
# subset
kmerg.sub <- kmerg[c("State","year","negsum","foxneg","AB3")]
# save out
#setwd("burden_data/")
write.csv(kmerg.sub, "medburden_measures.csv", row.names=F)